﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using cpe.cpein.DATA;

namespace cpe.cpein.BUSINESS
{
    public static class BUMessage
    {
        /// <summary>
        /// Get All the Message of the database.
        /// </summary>
        /// <param name="searchInDeleted"></param>
        /// <returns></returns>
        public static List<Message> GetAllMessage(bool searchInDeleted)
        {
            List<Message> Messages = new List<Message>();
            using (cpeinEntities db = new cpeinEntities())
            {
                var results = from Message in db.Message
                              where Message.msg_isdeleted == searchInDeleted
                              select Message;
                foreach (Message tmp in results)
                    Messages.Add(tmp);
            }
            return Messages;
        }

        /// <summary>
        /// Get the Message by id.
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static Message GetMessageByID(int id)
        {
            if (id <= 0)
                return null;
            else
            {
                using (cpeinEntities db = new cpeinEntities())
                {
                    var msg = from Message in db.Message
                              where Message.msg_id == id
                              select Message;
                    foreach (Message tmp in msg)
                        return tmp;
                    return null;
                }
            }
        }

        /// <summary>
        /// msg a Message into the database.
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>
        public static Message AddMessage(Message msg)
        {
            if (msg != null)
            {
                using (cpeinEntities db = new cpeinEntities())
                {
                    Message nmsg = db.Message.Add(msg);
                    db.SaveChanges();
                    return nmsg;
                }
            }
            else
                return null;
        }

        /// <summary>
        /// Remove a specific Message of the database.
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>
        public static bool RemoveMessage(Message msg)
        {
            if (msg != null)
            {
                msg.msg_isdeleted = true;
                if (UpdateMessage(msg) != null)
                {
                    using (cpeinEntities db = new cpeinEntities())
                    {
                        db.SaveChanges();
                        return true;
                    }
                }
            }

            return false;
        }

        /// <summary>
        /// Update the Message into the database.
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>
        public static Message UpdateMessage(Message msg)
        {
            if (msg != null)
            {
                using (cpeinEntities db = new cpeinEntities())
                {
                    var current = from Message in db.Message
                                  where Message.msg_id == msg.msg_id
                                  select Message;

                    foreach (var tmp in current)
                    {
                        tmp.msg_content = msg.msg_content;
                        tmp.msg_isprivate = msg.msg_isprivate;
                        tmp.msg_receiver_del = msg.msg_receiver_del;
                        tmp.msg_receiver_read = msg.msg_receiver_read;
                        tmp.msg_sender_del = msg.msg_sender_del;
                        tmp.msg_subject = msg.msg_subject;
                        tmp.usr_receiver_id = msg.usr_receiver_id;
                        tmp.usr_sender_id = msg.usr_sender_id;
                        tmp.msg_isdeleted = msg.msg_isdeleted;

                        db.SaveChanges();
                    }

                    msg = current as Message;
                }
            }

            return msg;
        }
    }
}
